import { _decorator, Component, Vec3, Node, Prefab, CCFloat, CCString, ColliderComponent } from 'cc';
import { poolManager } from '../../../../Framework/Scripts/Managers/poolManager';
import { enemyBase } from '../enemyBase';

import { boss_aoeBomb } from './boss_aoeBomb';
import { flySkillCollider } from './flySkillCollider';

//投石器 蜘蛛 抛物线
const { ccclass, property } = _decorator;
@ccclass('FireBall')
export class FireBall extends Component {
    @property({ type: Prefab, displayName: "落地爆炸" })
    public boomNode: Prefab = null

    @property({ type: CCFloat, displayName: "伤害" })
    public hitNum: number = 100


    @property({ displayName: "发射声音" })
    public soundName: string = "fireBall"



    public howHit: Node = null
    public speed: number = 10
    public isPlayHitFireBall: boolean = false;//是否播放火球
    public groundWorPosY: number = 1.8;//地面相对世界原点高度
    public scriptWarning: any = null!;//预警技能脚本
    public skillInfo: any = null!;//技能信息

    private _isAutoRotate: boolean = true;//方向是否自动调整
    private _posStart: Vec3 = new Vec3();//开始位置
    private _posEnd: Vec3 = new Vec3();//结束位置
    private _posOffset: Vec3 = new Vec3();//开始和结束位置差
    private _totalFlyTime: number = 0;//小火球飞行时间
    private _maxFlyHeight: number = 0;//小火球最大飞行高度
    private _curFlyTime: number = 0;//当前飞行时间
    private _rotateCoolTime: number = 0;//每隔0.1秒调整角度
    private _posNextTarget: Vec3 = new Vec3();//下次的目标位置
    private _targetPos: Vec3 = new Vec3();//目标位置

    private _fun: Function = null

    start() {

    }

    /**
    * 初始化 
    */
    public init(speed: number, startPos: Vec3, endPos: Vec3, max_height: number = 3, howHit: Node, fun?: Function) {
        if (fun) {
            this._fun = fun
        }

         if(this.node.getComponent(flySkillCollider)){//如果是飞行的物体 碰撞建筑物要判断是谁发射的 如过是我方发射则不爆炸
            this.node.getComponent(flySkillCollider).myType=howHit.getComponent(enemyBase).obj_type
         }
        this.howHit = howHit
        this.speed = speed;

        this._totalFlyTime = 0;
        this._maxFlyHeight = 0;
        this._curFlyTime = 0;

        this._posStart.set(new Vec3(startPos.x, this.groundWorPosY, startPos.z));
        this._posEnd.set(endPos);
        Vec3.subtract(this._posOffset, this._posEnd, this._posStart);
        this._totalFlyTime = this._posOffset.length() / speed
        this._maxFlyHeight = this._totalFlyTime * max_height;//最大飞行高度跟飞行距离成正比     
        this.isPlayHitFireBall = false;

        this.node.children.forEach((ndChild: Node) => {
            ndChild.active = true;
        })

        // SoundMgr.Instance.playSound(ResMgr.Instance.getAsset(constant.SOUNDS_GUI,this.soundName))

    }

    update(deltaTime: number) {

        //向指定目标飞行
        if (this._totalFlyTime > 0 && this.node.parent) {

            if (this._curFlyTime < this._totalFlyTime) {
                this._curFlyTime += deltaTime;
                this._curFlyTime = this._curFlyTime >= this._totalFlyTime ? this._totalFlyTime : this._curFlyTime;

                let percent = Number((this._curFlyTime / this._totalFlyTime).toFixed(2));
                //曲线飞行
                let height = this._maxFlyHeight * Math.cos(percent * Math.PI - Math.PI / 2);
                this._targetPos.set(this._posStart.x + this._posOffset.x * percent, this._posStart.y + height, this._posStart.z + this._posOffset.z * percent);

                this.node.setWorldPosition(this._targetPos);

                if (this._isAutoRotate) {
                    this._rotateCoolTime -= deltaTime;
                    if (this._rotateCoolTime < 0) {
                        this._rotateCoolTime = 0.1;
                        percent = Number(((this._curFlyTime + deltaTime) / this._totalFlyTime).toFixed(2));
                        if (percent < 1) {
                            //曲线飞行
                            height = this._maxFlyHeight * Math.cos(percent * Math.PI - Math.PI / 2);
                            this._posNextTarget.set(this._posStart.x + this._posOffset.x * percent, this._posStart.y + height, this._posStart.z + this._posOffset.z * percent)
                            this.node.forward = this._posNextTarget.subtract(this._targetPos).normalize();
                        }
                    }
                }

                //小火球碰到地面
                if (Number((this.node.position.y).toFixed(2)) <= this.groundWorPosY && !this.isPlayHitFireBall && this._curFlyTime > 0) {
                      this.bomb()
                    // this.isPlayHitFireBall = true;

                    // if (this._fun) {
                    //     this._fun()
                    //     this._fun = null
            
                    // }
            
            
            
            
                    // this.node.children.forEach((ndChild: Node) => {
                    //     ndChild.active = false;
                    // })
            
            
            
            
            
                    // let bomb: Node = poolManager.instance.getNode(this.boomNode, this.node.parent)
            
                    // bomb.setWorldPosition(this._posEnd)
            
                    // let ts: boss_aoeBomb = bomb.getComponent(boss_aoeBomb)
                    // if (ts) {
                    //     ts.hitNum = this.hitNum
                    //     ts.init(this.howHit)
                    // }
                    // poolManager.instance.putNode(this.node);

                }
            }
        }
    }

    public bomb(){
        this.isPlayHitFireBall = true;

        if (this._fun) {
            this._fun()
            this._fun = null

        }




        this.node.children.forEach((ndChild: Node) => {
            ndChild.active = false;
        })





        let bomb: Node = poolManager.instance.getNode(this.boomNode, this.node.parent)

        bomb.setWorldPosition(this.node.worldPosition)

        let ts: boss_aoeBomb = bomb.getComponent(boss_aoeBomb)
        if (ts) {
          //  console.log("飞行武器hit"+this.hitNum);
            
            ts.hitNum = this.hitNum
            ts.init(this.howHit)
        }
        poolManager.instance.putNode(this.node);
    }
}